package com.noriental.okcalls.controller;

import com.alibaba.fastjson.JSON;
import com.noriental.okcalls.utils.IpUtils;
import com.noriental.okcalls.utils.RandomUtils;
import com.noriental.okcalls.utils.SignUtils;
import com.noriental.okcalls.utils.XmlHelper;
import com.noriental.okcalls.wechat.api.pay.PaymentApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/***
 * *   ____  ___________  ___________           ________   ____  __.  _____ _____.___.
 * *   \   \/  /\______ \ \_   _____/           \_____  \ |    |/ _| /  _  \\__  |   |
 * *    \     /  |    |  \ |    __)     ______   /   |   \|      <  /  /_\  \/   |   |
 * *    /     \  |    `   \|     \     /_____/  /    |    \    |  \/    |    \____   |
 * *   /___/\  \/_______  /\___  /              \_______  /____|__ \____|__  / ______|
 * *    	 \_/        \/     \/                       \/        \/       \/\/
 * *
 * *   功能描述：
 * *
 * *   @DATE    2020-10-10
 * *   @AUTHOR  qiyubin
 ***/
@Slf4j
@Controller
public class WxPayController {

    //wxpay
    private static String appid = "";
    private static String partner = "222";
    private static String paternerKey = "333";
    private static String notify_url = "http://www.xxx.com/pay/pay_notify";

    @RequestMapping(value = "/buy")
    public String sendMsg(Map<String, Object> model, HttpServletRequest req) {

        // openId，采用 网页授权获取 access_token API：SnsAccessTokenApi获取
        String openId = "";

        // 统一下单文档地址：https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1

        Map<String, String> params = new HashMap<String, String>();
        params.put("appid", appid);
        params.put("mch_id", partner);
        params.put("body", "JFinal2.0极速开发");
        params.put("out_trade_no", "977773682111");
        params.put("total_fee", "1");

        String ip = IpUtils.getRealIp(req);
        if (StringUtils.isBlank(ip)) {
            ip = "127.0.0.1";
        }

        params.put("spbill_create_ip", ip);
        params.put("trade_type", PaymentApi.TradeType.JSAPI.name());
        params.put("nonce_str", RandomUtils.genNonceStr());
        params.put("notify_url", notify_url);
        params.put("openid", openId);

        String sign = SignUtils.createSign(params, paternerKey);
        params.put("sign", sign);
        String xmlResult = PaymentApi.pushOrder(params);

        System.out.println(xmlResult);
        Map<String, String> result = XmlHelper.xmlToMap(xmlResult);

        String return_code = result.get("return_code");
        String return_msg = result.get("return_msg");
//        if (StringUtils.isBlank(return_code) || !"SUCCESS".equals(return_code)) {
//            return return_msg;
//        }
//        String result_code = result.get("result_code");
//        if (StringUtils.isBlank(result_code) || !"SUCCESS".equals(result_code)) {
//            return return_msg;
//        }
        // 以下字段在return_code 和result_code都为SUCCESS的时候有返回
        String prepay_id = result.get("prepay_id");

        Map<String, String> packageParams = new HashMap<String, String>();
        packageParams.put("appId", appid);
        packageParams.put("timeStamp", System.currentTimeMillis() / 1000 + "");
        packageParams.put("nonceStr", System.currentTimeMillis() + "");
        packageParams.put("package", "prepay_id=" + prepay_id);
        packageParams.put("signType", "MD5");
        String packageSign = SignUtils.createSign(packageParams, paternerKey);
        packageParams.put("paySign", packageSign);

        String jsonStr = JSON.toJSONString(packageParams);
        model.put("json", jsonStr);
        return "pay";
    }


}
